package com.hong.utils.phaser;

import java.util.concurrent.Phaser;
import java.util.concurrent.TimeUnit;

/**
 * @Auther: Administrator
 * @Date: 2018/7/25 18:41
 * @Description:
 * 关于phaser
 */
public class PhaserExample4 {

    public static void main(String[] args) throws InterruptedException {
//        final Phaser phaser = new Phaser(1);
        /*到达这个移相器，等待其他人。*/
       /* phaser.arriveAndAwaitAdvance();
        System.out.println(phaser.getPhase());

        phaser.arriveAndAwaitAdvance();
        System.out.println(phaser.getPhase());

        phaser.arriveAndAwaitAdvance();
        System.out.println(phaser.getPhase());*/
        /*返回在此移动设备上注册的各方数量*/
      /*  System.out.println(phaser.getRegisteredParties());

        phaser.register();

        System.out.println(phaser.getRegisteredParties());

        phaser.register();

        System.out.println(phaser.getRegisteredParties());*/
        /*已注册数量*/
//        System.out.println(phaser.getArrivedParties());
        /*返回尚未到达此移相器当前阶段的已注册方的数量。*/
//        System.out.println(phaser.getUnarrivedParties());
        /*增加给定数量的新的有争议的派对到这个移相器。*/
//        phaser.bulkRegister(10);
//        System.out.println(phaser.getRegisteredParties());
//        System.out.println(phaser.getArrivedParties());
//        System.out.println(phaser.getUnarrivedParties());
//        new Thread(phaser::arriveAndAwaitAdvance).start();
//        TimeUnit.SECONDS.sleep(1);
//        System.out.println("==============================");
//        System.out.println(phaser.getRegisteredParties());
//        System.out.println(phaser.getArrivedParties());
//        System.out.println(phaser.getUnarrivedParties());

        final Phaser phaser = new Phaser(2){
            @Override
            protected boolean onAdvance(int phase, int registeredParties) {
                return false;
            }
        };

        new OnAdvanceTask("Alex",phaser).start();
        new OnAdvanceTask("Jack",phaser).start();

        TimeUnit.SECONDS.sleep(2);
        System.out.println(phaser.getUnarrivedParties());
        System.out.println(phaser.getArrivedParties());

    }

    static class OnAdvanceTask extends Thread{

        private final String name;

        private final Phaser phaser;

        OnAdvanceTask(String name,  Phaser phaser) {
            super(name);
            this.name = name;
            this.phaser = phaser;
        }


        @Override
        public void run() {
            System.out.println(getName()+ "I am start and the phase ph" +phaser.getPhase());
            phaser.arriveAndAwaitAdvance();
            System.out.println(getName()+ " I am end and!");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            /*返回 true如果移相器已被终止。*/
            System.out.println("isTerminated=>"+phaser.isTerminated());
            if(getName().equals("Alex")){
                System.out.println(getName()+ "I am start and the phase ph" +phaser.getPhase());
                phaser.arriveAndAwaitAdvance();
                System.out.println(getName()+ " I am end and!");
            }
        }
    }
}
